#include<stdio.h>
#include<string.h>
#include<errno.h>
#include<stdlib.h>
#include<unistd.h>

int main()
{
pid_t id=fork();
if(id==0)
{//子进程
  int cnt=5;
  while(cnt)
  {
    printf("I am child process,pid is %d,ppid is %d\n",getpid(),getppid());
    sleep(1);
    cnt--;
  }
    exit(10);//子进程执行到此退出
}
int status=0;
while(1)
{
  pid_t rid=waitpid(id,&status,WNOHANG);
  if(rid>0)
  {
    printf("wait success child pid is %d,exit code is %d\n",rid,WEXITSTATUS(status));
    break;
  }
  else if(rid==0)
  {
    printf("child is running,father can do other thing\n");
  }
  else
  {
    perror("waitpid");
    break;
  }
  sleep(1);
}
  return 0;
}
//int main()
//{
//pid_t id=fork();
//if(id==0)
//{//子进程
//  int cnt=5;
//  while(cnt)
//  {
//    printf("I am child process,pid is %d,ppid is %d\n",getpid(),getppid());
//    sleep(1);
//    cnt--;
//  }
//  int*p=NULL;
//  *p=100;
//    exit(10);//子进程执行到此退出
//}
//int status=0;
//waitpid(id,&status,0);
//if(WIFEXITED(status))//wifexited=wait if exited 等待如果正常退出
//{
//  printf("wait success,child pid is %d,status is %d,exit code is %d\n",id,status,WEXITSTATUS(status));//wexitstatus=wait exit status 等待退出状态
//}
//else
//{
//  printf("wait success,child pid is %d,status is %d,exit signal is %d\n",id,status,WTERMSIG(status);//wtermsig=wait terminate signal 等待终止信号
//}
//  return 0;
//  }
//int main()
//{
//pid_t id=fork();
//if(id==0)
//{//子进程
//  int cnt=5;
//  while(cnt)
//  {
//    printf("I am child process,pid is %d,ppid is %d\n",getpid(),getppid());
//    sleep(1);
//    cnt--;
//  }
//    exit(10);//子进程执行到此退出
//}
//printf("father begins waitting\n");
//pid_t rid=wait(NULL);//阻塞等待
//if(rid>0)
//{
//  printf("father wait success\n");
//}
//  return 0;
//}
//void print()
//{
//  printf("hello linux");
//  _exit(1);
//}
//int main()
//{ 
//  printf("hello linux");
//  exit(1); 
//}
//int main()
//{
//  FILE* fp=fopen("./aaa.txt","r");
//  printf("%d:%s\n",errno,strerror(errno));
//  return 0;
//}
//enum{
//  success,
//  r_err,
//  w_err,
//  x_err
//};
//const char*whaterr(int code)
//{
//  switch(code)
//  {
//    case success:
//      return "success";
//    case r_err:
//      return "read error";
//    case w_err:
//      return "write error";
//    case x_err:
//      return "execution error";
//  }
//}
//int main()
//{
//  int code=x_err;
//  printf("%s\n",whaterr(code));
//
//  return 0;
//}
//int main()
//{
//for(int i=0;i<200;i++)
//{
//  printf("%d:%s\n",i,strerror(i));
//}
//  return 0;
//}
